﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI;
using Microsoft.SharePoint;
using RENIT.DragNDropUpload.Extensibility;

namespace RENIT.DragNDropUpload.Utils
{
    public static class WebPartsAnalizer
    {
        public static List<UploadTargetData> GetUploadTargets(Page page)
        {
            var contextUser = SPContext.Current.Web.CurrentUser;
            var manager = SPWebPartManager.GetCurrentWebPartManager(page);
            if (manager == null)
            {
                return new List<UploadTargetData>();
            }
            var result = new List<UploadTargetData>();
            foreach (System.Web.UI.WebControls.WebParts.WebPart part in manager.WebParts)
            {
                var data = WebPartsAdaptersManager.GetTargetData(part);
                if (data != null)
                {
                    result.Add(data);
                }
            }
            return result;
        }


        static WebPartsAnalizer()
        {
            WebPartsAdaptersManager.OnAdaptersInitCallback += WebPartsAdaptersManager_OnAdaptersInitCallback;
        }

        static void WebPartsAdaptersManager_OnAdaptersInitCallback()
        {
            WebPartsAdaptersManager.InitAdapters(new ListViewWebPartsAdapter(), new XsltListViewWebPartAdapter());
            if (AssebliesDetector.IsLoaded("Microsoft.Office.DocumentManagement, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"))
            {
                WebPartsAdaptersManager.InitAdaptersDynamically("RENIT.DragNDropUpload.SP2010.DocumentSetsIntegration.DocumentSetContentsWebPartAdapter, RENIT.DragNDropUpload.SP2010.DocumentSetsIntegration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d89268ccb896cff0");
            }
        }

    }
}
